﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Platform.Storage.Redis.Messages
{
	/// <summary>
	/// Uma coleção de itens retornados pelo REDIS
	/// </summary>
	public sealed class CollectionRedisMessage : RedisMessageBase<IEnumerable<string>>
	{

		public CollectionRedisMessage( RedisMessageChannel channel, string command, string[] parameters )
			: base( channel, command, parameters )
		{ }

		protected override IEnumerable<string> GetReply( Func<RedisReplyLine> nextLine )
		{
			var header = nextLine();

			if ( header.Reply == RedisReplyStatus.Collection )
			{
				int len = Convert.ToInt32( header.Message );
				List<string> lines = new List<string>( len );

				while ( len-- > 0 )
				{
					var item = nextLine();
					if ( item.Reply == RedisReplyStatus.CollectionItem )
						lines.Add( item.Message );
				}

				base.ReplyStatus = RedisReplyStatus.Collection;
				return lines;
			}
			else
				return Enumerable.Empty<String>();
		}
	}
}
